package com.example.Handler;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.examplee.username.httpfrist_01.R;
//Handler负责发送消息，Looper是接收HAndler的消息，
public class HandlerLoopActivity extends AppCompatActivity {
Handler handler=new Handler(){

    @Override
    public void handleMessage(Message msg) {
        System.out.println("UI----->"+Thread.currentThread());
    }
};
    //这里的 Handler hander在子线程中
    class MyThread extends Thread{
        Handler hander;


        @Override
        public void run() {
            Looper.prepare();
            hander=new Handler(){

                @Override
                public void handleMessage(Message msg) {
                   System.out.println("currentThread"+Thread.currentThread());
                }
            };
            Looper.loop();
        }
    }
    MyThread thread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView=new TextView(this);
        textView.setText("Handler线程");
        setContentView(textView);

        thread=new MyThread();
        thread.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.hander.sendEmptyMessage(1);
        handler.sendEmptyMessage(1);
    }
}
